import { createRouter, createWebHistory } from 'vue-router'
import { useUserInfoStore } from '@/stores/UserInfo.ts'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/pages/HomePage.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginPage.vue'),
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterPage.vue'),
    },
    {
      path: '/questionBank',
      name: 'questionBank',
      component: () => import('../views/pages/problem/QuestionBankPage.vue'),
    },
    {
      path: '/problem/detail/:id',
      name: 'problemDetail',
      component: () => import('../views/pages/problem/QuestionInfoPage.vue'),
      props: true
    },
    {
      path: '/assessment',
      name: 'assessment',
      component: () => import('../views/pages/AssessmentPage.vue'),
    },
    {
      path: '/rank',
      name: 'rank',
      component: () => import('../views/pages/RankPage.vue'),
    },
    {
      path: '/personal',
      name: 'personalCenter',
      component: () => import('../views/pages/user/CenterPage.vue'),
    },
    {
      path: '/organization',
      name: 'organization',
      component: () => import('../views/pages/organization/OrganizationsPage.vue'),
    },
    {
      path: '/user/edit/:userId?',
      name: 'userEdit',
      component: () => import('../views/pages/user/EditPage.vue'),
    },
    {
      path: '/admin/center',
      name: 'adminCenter',
      component: () => import('../views/admin/CenterPage.vue'),
      meta: { requiresAdmin: true },
    },
    {
      path: '/admin/user',
      name: 'UserManage',
      component: () => import('../views/admin/user/UserManage.vue'),
      meta: { requiresAdmin: true },
    },
    {
      path: '/admin/problem',
      name: 'ProblemManage',
      component: () => import('../views/admin/problem/ProblemManage.vue'),
      meta: { requiresAdmin: true },
    },
    {
      path: '/admin/problem/add',
      name: 'ProblemAdd',
      component: () => import('../views/admin/problem/components/AddDialog.vue'),
      meta: { requiresAdmin: true },
    },
    {
      path: '/admin/problem/edit/:id',
      name: 'ProblemEdit',
      component: () => import('../views/admin/problem/components/UpdateDialog.vue'),
      meta: { requiresAdmin: true },
    },
    {
      path: '/admin/organization',
      name: 'OrganizationManage',
      component: () => import('../views/admin/organization/OrganizationManage.vue'),
      meta: { requiresAdmin: true },
    },
    {
      path: '/admin/images',
      name: 'ImagesManage',
      component: () => import('../views/admin/images/ImageManage.vue'),
      meta: { requiresAdmin: true },
    },
  ],
})

router.beforeEach((to, from, next) => {
  const userStore = useUserInfoStore()
  if (to.meta?.requiresAdmin && !userStore.isAdmin()) {
    next({ name: 'home' })
    return
  }
  next()
})
export default router
